<template>
    <div class="answer-container">
      <div class="main">
        <resource-panel
          :task-name="trainingName"
          @changeIndex="changeIndex"
          :subject-num="subjectDetail.subjectList.length"
          :resource-list="subjectDetail.titleList ? subjectDetail.titleList : []"></resource-panel>
        <subject-panel
          @subjectItemSl="subjectSlAnswer"
          @subjectItemInput="subjectInputAnswer"
          @changeIndex="changeIndex"
          :stu-node-case-id="stuNodeCaseId"
          :stu-task-case-id="stuTaskCaseId"
          :subject="subjectDetail.subjectList"
          style="margin-left: 19px"></subject-panel>
      </div>
    </div>
</template>

<script>
import resourcePanel from './resourcePanel'
import subjectPanel from './subjectPanel'
export default {
  name: 'answerPanel',
  props: {
    subjectDetail: Object,
    trainingName: String,
    trainingId: String,
    stuTaskCaseId: String,
    stuNodeCaseId: String
  },
  data () {
    return {
      activeSubjectIndex: 0
    }
  },
  methods: {
    changeIndex (data) {
      this.activeSubjectIndex = data
    },
    subjectSlAnswer (code, subjectIndex) {
      let newVal = JSON.parse(JSON.stringify(this.subjectDetail.subjectList[subjectIndex]))
      const subjectType = parseInt(newVal.subject_type)
      // 需要计算出分数
      switch (subjectType) {
        case 10:
          newVal.slCode = code
          this.setType10IsRight(code, newVal)
          break
        case 11:
          let oldSlCode = newVal.slCode
          if (this._.isEmpty(oldSlCode)) {
            oldSlCode = ''
          }
          let oldSlCodeArray = []
          if (oldSlCode) oldSlCodeArray = oldSlCode.split(',')

          let hasSl = false
          for (let i = 0; i < oldSlCodeArray.length; i++) {
            if (oldSlCodeArray[i] === code) {
              oldSlCodeArray.splice(i, 1)
              hasSl = true
              break
            }
          }
          if (!hasSl) {
            oldSlCodeArray.push(code)
          }
          newVal.slCode = oldSlCodeArray.join(',')
          this.setType11IsRight(oldSlCodeArray.join(','), newVal)
          break
        case 12:
          newVal.slCode = code
          this.setType12IsRight(code, newVal)
          break
        default:
          throw new Error('subject类型处理器未设置【#1000】')
      }
      this.$set(this.subjectDetail.subjectList, subjectIndex, newVal)
    },
    setType10IsRight (code, subject) {
      subject.right_flag = 0
      subject.score = 0
      if (code === subject.right_answer) {
        subject.right_flag = 1
        subject.score = 100
      }
    },
    setType11IsRight (code, subject) {
      subject.right_flag = 0
      subject.score = 0
      const stuCodeArray = code.split(',').sort()
      const rightCodeArray = subject.right_answer.split(',').sort()
      if (this._.isEqual(stuCodeArray, rightCodeArray)) {
        subject.right_flag = 1
        subject.score = 100
      }
    },
    setType12IsRight (code, subject) {
      subject.right_flag = 0
      subject.score = 0
      if (code === subject.right_answer) {
        subject.right_flag = 1
        subject.score = 100
      }
    },
    subjectInputAnswer (val, subjectIndex, subjectItemIndex) {
      let newVal = JSON.parse(JSON.stringify(this.subjectDetail.subjectList[subjectIndex]))
      let answerList = newVal.answerList || []
      newVal.answerList = answerList
      this.$set(this.subjectDetail.subjectList, subjectIndex, newVal)
    }
  },
  components: {
    resourcePanel, subjectPanel
  }
}
</script>

<style scoped lang="scss">
.answer-container{
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  .main{
    width: 1200px;
    min-height: 100%;
    display: flex;
    justify-content: space-between;
  }
}
</style>
